﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class charButton : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
    [SerializeField]
    private Image icon;
    [SerializeField]
    private Image inactiveIcon;
 
    [SerializeField]
    private EquipmentType type;
    private Equipment equippedEquipment;
    [SerializeField]
    private GearSoket soket;

    public Equipment MyEquippedEquipment { get => equippedEquipment; set => equippedEquipment = value; }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            if (handScript.MyInstance.MyMovable is Equipment)
            {
                Equipment tmpEquip = (Equipment)handScript.MyInstance.MyMovable;
                if (tmpEquip.MyType == type)
                {
                    EquipArmor(tmpEquip);

                }
                UIManager.MyInstance.refreshTooltip(tmpEquip);
            }
            else if (MyEquippedEquipment!=null && handScript.MyInstance.MyMovable==null)
            {
                handScript.MyInstance.takeMovable(MyEquippedEquipment);
                icon.color = Color.grey;
                CharcterPanel.MyInstance.MyCharbutton = this;
            }
        }
    }
    public void EquipArmor(Equipment eqp)
    {
        eqp.ItemDelete();
        if(MyEquippedEquipment != null)
        {
            if (MyEquippedEquipment != eqp)
            {
                inventoryScript.MyInstance.AddItem(MyEquippedEquipment);
            }
            

            UIManager.MyInstance.refreshTooltip(MyEquippedEquipment);
        }
        else
        {
            UIManager.MyInstance.hideToolTip();
        }
        icon.enabled=true;
        icon.color = Color.white;
        inactiveIcon.enabled=false;
        icon.sprite = eqp.Icon;
        this.MyEquippedEquipment = eqp;
        this.MyEquippedEquipment.MyCharButton = this;
        //handScript.MyInstance.MyMovable = null;
        if(handScript.MyInstance.MyMovable==(eqp as IMovable))
        {
            handScript.MyInstance.Drop();
        }
        if(soket!=null&&equippedEquipment.MyAnimations != null)
        {
            soket.Equip(equippedEquipment.MyAnimations);
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        UIManager.MyInstance.hideToolTip();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if(MyEquippedEquipment != null)
        {
            UIManager.MyInstance.showToolTip(new Vector2(0, 0), transform.position,MyEquippedEquipment);
        }
        
    }
    public void Unequip()
    {
        if (soket != null && equippedEquipment.MyAnimations != null)
        {
            soket.UnEquip();
        }
        icon.color = Color.white;
        icon.enabled = false;
        inactiveIcon.enabled = true;
        MyEquippedEquipment.MyCharButton = null;
        MyEquippedEquipment = null;
        
    }
}
